local skel = fk.CreateSkill {
  name = "emo__huizhan",
  tags = {Skill.Compulsory},
}

Fk:loadTranslationTable{
  ["emo__huizhan"] = "回斩",
  [":emo__huizhan"] = "锁定技，你的额定回合结束时，你执行一个所有角色仅能使用共计X张牌的额外回合（X为你本回合使用牌数）。",
  ["@emo__huizhan-turn"] = "回斩",
}

skel:addEffect(fk.TurnEnd, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    return player == target and player:hasSkill(skel.name) and not player:insideExtraTurn()
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local n = #room.logic:getEventsOfScope(GameEvent.UseCard, 999, function(e)
      local use = e.data
      return use.from == player
    end, Player.HistoryTurn)
    if n == 0 then return end
    player:gainAnExtraTurn(true, skel.name, nil, { emo__huizhan = n })
  end,
})

skel:addEffect(fk.TurnStart, {
  can_refresh = function (self, event, target, player, data)
    return player == target and data.extra_data and data.extra_data.emo__huizhan
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "@emo__huizhan-turn", data.extra_data.emo__huizhan)
  end,
})

skel:addEffect(fk.AfterCardUseDeclared, {
  can_refresh = function (self, event, target, player, data)
    return tonumber(player:getMark("@emo__huizhan-turn")) ~= 0
  end,
  on_refresh = function (self, event, target, player, data)
    local n = tonumber(player:getMark("@emo__huizhan-turn")) - 1
    player.room:setPlayerMark(player, "@emo__huizhan-turn", n > 0 and n or "0")
  end,
})

skel:addEffect("prohibit", {
  prohibit_use = function(self, player, card)
    return table.find(Fk:currentRoom().alive_players, function (p)
      return p:getMark("@emo__huizhan-turn") == "0"
    end) ~= nil
  end,
})


return skel
